<?php

namespace common\behavior\CommonMessagePushing;


use common\model\EntityModel;
use common\service\BehaviorService;
use common\service\ReviewRequestMessageService;
use common\service\UserService;
use Webman\Stomp\Client;

/**
 * 延期待审核 消息通知
 */
class DelayReviewBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'review_request' => '',
                'review_request_node' => '',
                'creator' => '',
            ],
            [
                'review_request' => [],
                'review_request_node' => [],
                'creator' => []
            ],
            'data',
            ['type' => 'task_delay_review'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($reviewRequest, $reviewRequestNode, $creator) = $params;
        if (empty($reviewRequest) || empty($reviewRequestNode) || empty($creator)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/
        $reviewManData = UserService::getInstance()->getUserFindField(['id' => $reviewRequestNode['plan_reviewer']], '*');
        $fields = [
            'entity.initial_episode_code', 'entity.showings_number', 'entity.name', 'entity.module_id',
            'task.name',
        ];
        $taskEntity = model(EntityModel::class)
            ->join('task on task.entity_id = entity.id')
            ->where(['task.id' => $reviewRequest['link_id']])
            ->field(build_complex_query_fields($fields, '__'))
            ->find();
        if (empty($taskEntity)) {
            return;
        }
        $taskEntity = separate_complex_query_data($taskEntity, '__');
        $reviewRequest['param'] = json_decode($reviewRequest['param'], true);
        $messageParam = [
            'first' => '有新的延期申请',
            'task_name' => get_chat_entity_task_name($taskEntity['entity'], $taskEntity['task']),
            'sender_name' => $creator['name'],
            'task_delay_time' => date("Y/m/d H:i", $reviewRequest['param']['task_delay_time']),
            'remark' => '请及时审核',
            'task_id' => $reviewRequest['link_id'],
            'review_request_id' => $reviewRequest['id'],
            'review_request_node_id' => $reviewRequestNode['id'],
        ];
        $messageTemplateId = ReviewRequestMessageService::getInstance()->taskDelayConfirmMessageTemplateId();

        /***********  发送新的队列  ************/
        Client::send('common_message_pushing', build_queue_data_with_xu([
            'message_generated' => [[
                "param" => $messageParam,
                'receiver' => $reviewManData['union_id'],
                'template_id' => $messageTemplateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_review',
                'is_popup' => 1,
                'message_key' => 'task_delay_need_review',
            ]],
        ]));
        /***********  结束  ************/
    }
}
